<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/7
 * Time: 9:23
 */

namespace Admin\Frontend\Controllers;

use Helpers\Main;
use Helpers\Assets;
use Services\Box;
use Modules\Power;
use Helpers\User;
use Helpers\Template;

class BoxController extends ControllerBase
{
    private static $online = [
        0 => '离线',
        1 => '在线',
    ];

    private static $type = [
        0 => '全部',
        1 => '未使用的',
        2 => '有异常的',
        3 => '空间不足',
        4 => '负载的',
    ];

    private static $business_type = [
        0 => 'apk',
        1 => '题库',
        2 => '其他',
    ];

    private static $health_status = [
        'SB103013' => [
            0 =>' 正确',
            1 =>' 检测盒子接口失败',
            2 => '下载文件接口失败',
            3 => '获取微服务失败'
        ],
        'SA104002' => [
            200 => '检测成功',
            300 => '通道连通检测失败',
            400 => '微服务检测失败',
            500 => 'redis 检测失败',
            600 => '访问外网联通性检测失败',
        ],
    ];

    /**
     * box列表
     * create by zqk 20180907
     */
    public function indexAction()
    {
        if (!Power::exist('Server_BoxList')) {
            return $this->sendViewError('无盒子管理权限！');
        }
        $this->di->get('assets')
            ->addCss('assets/sweetalert2/sweetalert2.min.css')
            ->addJs('assets/sweetalert2/sweetalert2.min.js');
        $this->view->pick('box/boxList');
        Main::showNavPosition();
        Assets::easyUI();
        Assets::importJs('js/box/boxList.js');
        Assets::importMainJs('assets/xh/xhTop.js');
        Assets::importMainJs('assets/easyui/datagrid-detailview.js');
        $boxSearchArr = [];
        $boxSearchArr[] = ['type' => 'text', 'id' => 'health', 'label' => ' health: '];
        $boxSearchArr[] = [
            'type' => 'select',
            'id' => 'online',
            'data' => ['' => '全部'] + self::$online,
            'default' => '1',
            'style' => 'margin-left:10px;width:60px;'
        ];
        $boxSearchArr[] = [
            'type' => 'select',
            'id' => 'boxType',
            'data' => self::$type,
            'default' => '0',
            'style' => 'margin-left:10px;width:80px;'
        ];
        $boxSearchArr[] = [
            'type' => 'button',
            'id' => 'bt_search',
            'text' => '查找',
            'icon' => 'fa-search',
            'style' => 'margin-left:10px'
        ];
        $buttonArr = [];
        $buttonArr[] = [
            'plain' => 'true',
            'id' => 'downError',
            'text' => '下载失败上报列表',
            'icon' => 'fa-list-ul',
        ];
        $buttonArr[] = [
            'plain' => 'true',
            'id' => 'downList',
            'text' => '下载上报列表',
            'icon' => 'fa-list-ul',
        ];
        $this->view->setVars([
            'boxList' => Template::createList('boxList', 'boxList', [
                'searchToolbar' => Template::createSearchToolBar($boxSearchArr, true),
                'buttonToolbar' => Template::createButtonToolBar($buttonArr),
            ]),
        ]);
    }

    /**
     * 获取盒子
     *
     *
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     * @throws \Helpers\XhException
     *
     * create by zqk 20180913
     */
    public function getBoxAction()
    {
        if (!Power::exist('Server_BoxList')) {
            return $this->sendErrorResponse(403, '无盒子管理权限！');
        }
        $online = $this->request->getPost('online', 'int', 1);
        $type = $this->request->getPost('type', 'int', 0);
        $schoolId = $this->request->getPost('schoolId', 'int', 0);
        $health = $this->request->getPost('health', 'int');//手动输入
        $page = $this->request->getPost('page', 'int', 1);
        $rows = $this->request->getPost('rows', 'int', 50);
        $start = $rows * ($page - 1);
        $data = Box::getBox($online, $type, $schoolId, $health, $start, $rows);
        $boxStatus = self::$health_status;
        foreach ($data as $k => $v) {
            $data[$k]['healthStatus'] =$boxStatus[$v['appId']][$v['healthStatus']];
        };
        $totalData = Box::getBoxNum($online, $type, $schoolId, $health);
        $total = count($totalData);
        $data = ['total' => $total, 'rows' => $data, "footer" => ["name" => "Total", "price" => 18]];
        return $this->sendJsonResponse($data);
    }

    /**
     * 修改盒子信息form
     *
     * create by zqk 20180913
     */
    public function boxFormAction()
    {
        if (!Power::exist('Server_BoxList_Edit')) {
            return $this->sendViewError('无编辑盒子权限！');
        }
        $schoolId = $this->request->getQuery('schoolId');
        $appId = $this->request->getQuery('appId');
        $code = $this->request->getQuery('code');
        $schoolName = $this->request->getQuery('schoolName');
        $downloadReportSwitch = $this->request->getQuery('downloadReportSwitch');
        $downloadFailedReportSwitch = $this->request->getQuery('downloadFailedReportSwitch');
        $downloadLimitRateSwitch = $this->request->getQuery('downloadLimitRateSwitch');
        $uploadLimitRateSwitch = $this->request->getQuery('uploadLimitRateSwitch');
        $downBandwidth = $this->request->getQuery('downBandwidth');
        $upBandwidth = $this->request->getQuery('upBandwidth');
        $mac = $this->request->getQuery('mac');
        Assets::easyUI();
        Assets::importJs('js/box/boxForm.js');
        $this->view->setVars([
            'schoolId' => $schoolId,
            'appId' => $appId,
            'code' => $code,
            'schoolName' => $schoolName,
            'downloadReportSwitch' => $downloadReportSwitch,
            'downloadFailedReportSwitch' => $downloadFailedReportSwitch,
            'downloadLimitRateSwitch' => $downloadLimitRateSwitch,
            'uploadLimitRateSwitch' => $uploadLimitRateSwitch,
            'downBandwidth' => $downBandwidth,
            'upBandwidth' => $upBandwidth,
            'mac' => $mac,
        ]);
        $this->view->pick('box/boxForm');
    }

    /**
     * 修改盒子信息
     *
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     * @throws \Exception
     *
     * create by zqk 20180913
     */
    public function saveBoxAction()
    {
        if (!Power::exist('Server_BoxList_Edit')) {
            return $this->sendErrorResponse(403, '无编辑盒子权限！');
        }
        $schoolId = $this->request->getPost('schoolId');
        $appId = $this->request->getPost('appId');
        $code = $this->request->getPost('code');
        $schoolName = $this->request->getPost('schoolName');
        $drs = $this->request->getPost('downloadReportSwitch');
        $dfrs = $this->request->getPost('downloadFailedReportSwitch');
        $ulrs = $this->request->getPost('uploadLimitRateSwitch');
        $dlrs = $this->request->getPost('downloadLimitRateSwitch');
        $dbw = $this->request->getPost('downBandwidth');
        $ubw = $this->request->getPost('upBandwidth');
        $mac = $this->request->getPost('mac');
        if (empty($schoolId)) {
            return $this->sendErrorResponse('400', '学校Id为空');
        }
        if (empty($appId)) {
            return $this->sendErrorResponse('400', '应用Id为空');
        }
        if (empty($code)) {
            return $this->sendErrorResponse('400', '编号为空');
        }
        if (empty($schoolName)) {
            return $this->sendErrorResponse('400', '学校名称为空');
        }
        if (empty($dbw)) {
            return $this->sendErrorResponse('400', '宽带下行为空');
        }
        if (empty($ubw)) {
            return $this->sendErrorResponse('400', '宽带上行为空');
        }
        $re = Box::updateBox($mac, $schoolId, $appId, $code, $schoolName, $drs, $dfrs, $ulrs, $dlrs, $dbw, $ubw);
        if ($re) {
            return $this->sendSuccessResponse('操作成功');
        } else {
            return $this->sendErrorResponse('400', '操作失败');
        }
    }

    /**
     * 盒子下载错误列表
     *
     * create by zqk 20180913
     */
    public function boxFileErrorListAction()
    {
        if (!Power::exist('Server_BoxList')) {
            return $this->sendViewError('无盒子管理权限！');
        }
        $this->di->get('assets')
            ->addCss('assets/sweetalert2/sweetalert2.min.css')
            ->addJs('assets/sweetalert2/sweetalert2.min.js');
        $this->view->pick('box/boxFileErrorList');
        Main::showNavPosition();
        Assets::easyUI();
        Assets::importJs('js/box/boxFileErrorList.js');
        Assets::importMainJs('assets/xh/xhTop.js');
        $searchArr = [];
        $searchArr[] = ['type' => 'text', 'id' => 'downUrl', 'label' => ' url: '];
        $searchArr[] = ['type' => 'text', 'id' => 'macId', 'label' => ' mac: '];
        $searchArr[] = [
            'type' => 'button',
            'id' => 'bt_search',
            'text' => '查找',
            'icon' => 'fa-search',
            'style' => 'margin-left:10px'
        ];
        $this->view->setVars([
            'boxFileErrorList' => Template::createList('boxFileErrorList', 'boxFileErrorList', [
                'searchToolbar' => Template::createSearchToolBar($searchArr)
            ]),
        ]);
    }

    /**
     * 获取盒子下载失败上报记录列表
     *
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     * @throws \Helpers\XhException
     *
     * create by zqk 20180913
     */
    public function getBoxFileErrorListAction()
    {
        if (!Power::exist('Server_BoxList')) {
            return $this->sendErrorResponse(403, '无盒子管理权限！');
        }
        $url = $this->request->getPost('url', 'string');
        $mac = $this->request->getPost('mac', 'string');
        $page = $this->request->getPost('page', 'int', 1);
        $rows = $this->request->getPost('rows', 'int', 50);
        $start = $rows * ($page - 1);
        $listData = Box::getBoxFileErrorList($mac, $url, $start, $rows);
        return $this->sendJsonResponse($listData);
    }

    /**
     * 获取maclist
     *
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     * @throws \Helpers\XhException
     *
     * create by zqk 20180913
     */
    public function getMacListAction()
    {
        $data = Box::getMacList();
        return $this->sendJsonResponse($data);
    }

    /**
     * 盒子下载列表
     *
     * create by zqk 20180913
     */
    public function boxFileListAction()
    {
        if (!Power::exist('Server_BoxList')) {
            return $this->sendViewError('无盒子管理权限！');
        }
        $this->di->get('assets')
            ->addCss('assets/sweetalert2/sweetalert2.min.css')
            ->addJs('assets/sweetalert2/sweetalert2.min.js');
        $this->view->pick('box/boxFileList');
        Main::showNavPosition();
        Assets::easyUI();
        Assets::importJs('js/box/boxFileList.js');
        Assets::importMainJs('assets/xh/xhTop.js');
        $searchArr = [];
        $searchArr[] = ['type' => 'text', 'id' => 'url', 'label' => ' url: '];
        $searchArr[] = ['type' => 'text', 'id' => 'tag', 'label' => ' tag: '];
        $searchArr[] = ['type' => 'text', 'id' => 'macId', 'label' => ' mac: '];
        $searchArr[] = [
            'label' => ' 业务类型:',
            'type' => 'select',
            'id' => 'businessType',
            'data' => ['' => '全部'] + self::$business_type,
            'style' => 'margin-left:10px;width:80px;'
        ];
        $searchArr[] = [
            'type' => 'button',
            'id' => 'bt_search',
            'text' => '查找',
            'icon' => 'fa-search',
            'style' => 'margin-left:10px'
        ];
        $this->view->setVars([
            'business_type' => self::$business_type,
            'boxFileList' => Template::createList('boxFileList', 'boxFileList', [
                'searchToolbar' => Template::createSearchToolBar($searchArr)
            ]),
        ]);
    }

    /**
     * 获取盒子下载列表数据
     *
     *
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     * @throws \Helpers\XhException
     *
     * create by zqk 20180913
     */
    public function getBoxFileListAction()
    {
        if (!Power::exist('Server_BoxList')) {
            return $this->sendErrorResponse(403, '无盒子管理权限！');
        }
        $url = $this->request->getPost('url', 'string');
        $mac = $this->request->getPost('mac', 'string');
        $type = $this->request->getPost('type', 'int');
        $tag = $this->request->getPost('tag', 'string');
        $page = $this->request->getPost('page', 'int', 1);
        $rows = $this->request->getPost('rows', 'int', 50);
        $start = $rows * ($page - 1);
        $listData = Box::getBoxFileList($mac, $url, $type, $tag, $start, $rows);
        return $this->sendJsonResponse($listData);
    }

}